//
//  BhDataBase.m
//  bh
//
//  Created by lanou on 15/12/28.
//  Copyright (c) 2015年 lanou. All rights reserved.
//

#import "BhDataBase.h"
static BhDataBase *bhDataBase;
@implementation BhDataBase
+ (BhDataBase *)shareSqliteBh {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        bhDataBase = [[BhDataBase alloc]init];
    });
    return bhDataBase;
}

- (NSString *)sqlitePaths:(NSString *)sqlitePaths {
    // 先设置路径，然后拼接数据库名称
    NSString *sqlPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)firstObject]stringByAppendingString:sqlitePaths];
    NSLog(@"%@",sqlPath);
    // 返回一个一路径+数据库名字的字符串给我们
    return sqlPath;
}

- (void)openSqliteWithPaths:(NSString *)paths {
    NSString *sqlitePath = [self sqlitePaths:paths];
    self.dataBase = [FMDatabase databaseWithPath:sqlitePath];
}

- (void)closeSqlite {
    [self.dataBase close];
}

// 创建一个数据库表
- (void)createSqliteTableWithName:(NSString *)name {
    // 判断_dataBase是否存在(是否打开)
    if (!_dataBase) {
        // 如果不存在，我们调用创建（打开）的方法
        [self openSqliteWithPaths:[NSString stringWithFormat:@"%@.db",name]];
    }
    if ([_dataBase open]) {
        BOOL bhName = [_dataBase executeUpdate:@"CREATE TABLE ShouCangList(UserID TEXT,Title TEXT,ImageView TEXT,PersonalPictureView TEXT,AddLabel TEXT)"];
        if (bhName) {
            NSLog(@"创建成功");
        }else {
            NSLog(@"建表失败");
        }
    }
}


// 插入一条数据
- (void)insertModel:(BrowseModel *)model {
    if (!model) {
        return;
    }
    
    if ([_dataBase open]) {
        NSString *inserModel = [NSString stringWithFormat:@"insert into ShouCangList(Title, UserID,ImageView ,PersonalPictureView ,AddLabel ) values(?,?,?,?,?)"];
        BOOL insertSqlite = [_dataBase executeUpdate:inserModel,model.title,model.ID,model.cover_pic,model.author_face,model.title_sub];
        if (insertSqlite) {
            NSLog(@"插入成功");
        } else {
            NSLog(@"插入失败");
        }
    }
}
// 删除一条数据
- (void)deleteWithSingId:(NSString *)ID {
    if ([_dataBase open]) {
        NSString *deleteSqlite = [NSString stringWithFormat:@"delete from ShouCangList where UserID = ?"];
        BOOL deleteBh = [_dataBase executeUpdate:deleteSqlite,ID];
        if (deleteBh) {
            NSLog(@"删除成功");
        } else {
            NSLog(@"删除失败");
        }
    }
}
// 获取全部数据
- (NSMutableArray *)selegetAllModel {
    NSMutableArray *allArray = [[NSMutableArray alloc]init];
    if ([_dataBase open]) {
        NSString *selegetAllSqlite = @"select * from ShouCangList";
        FMResultSet *resultSet = [_dataBase executeQuery:selegetAllSqlite];
        while ([resultSet next]) {
            BrowseModel *model = [[BrowseModel alloc]init];
             model.ID = [resultSet stringForColumn:@"UserID"];
            model.title = [resultSet stringForColumn:@"title"];
            model.cover_pic = [resultSet stringForColumn:@"ImageView"];
            model.author_face = [resultSet stringForColumn:@"PersonalPictureView"];
            model.title_sub = [resultSet stringForColumn:@"AddLabel"];
            [allArray addObject:model];
           
        }
    }
    return allArray;
}












@end








